package com.persistence.dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.model.Stockmateriaprima;
import com.persistence.dao.DAOException;
import com.persistence.dao.DAOManagerMain;
import com.persistence.dao.interfaces.DAOStockmateriaprima;

public class MySQLDAOStockmateriaprima implements DAOStockmateriaprima {
	
	private Stockmateriaprima stockmateriaprima = null;
	
	
	public MySQLDAOStockmateriaprima() {
	}

	public int insert(Stockmateriaprima stockmateriaprima) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("insert into Stockmateriaprima ("
					+ "Id_Stockmateriaprima, Stockactual "
					+ ") " + "values (?, ?) ");

			stmt.setInt(1, stockmateriaprima.getNroMateriaprima());
			stmt.setInt(2, stockmateriaprima.getStockactual());
			stmt.executeUpdate();
			
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);

		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int update(Stockmateriaprima stockmateriaprima) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("update materiaprima " 
							+ "set Stockactual = ?"
							+ " where id_materiaprima = ? ");

			stmt.setInt(1, stockmateriaprima.getStockactual());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int eliminate(Stockmateriaprima stockmateriaprima) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("delete from materiaprima where id_materiaprima = ? ");
			stmt.setLong(1, stockmateriaprima.getNroMateriaprima());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public ArrayList<Stockmateriaprima> select() throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		ArrayList<Stockmateriaprima> stockmateriaprimas = new ArrayList<Stockmateriaprima>();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("select * from materiaprima");
			rs = stmt.executeQuery();

			while (rs.next()) {
				Stockmateriaprima stockmateriaprima = new Stockmateriaprima();
				stockmateriaprima.setNroMateriaprima(rs.getInt("Id_materiaprima"));
				stockmateriaprima.setStockactual(rs.getInt("Stockactual"));
				stockmateriaprimas.add(stockmateriaprima);
			}

			return stockmateriaprimas;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public Stockmateriaprima findById(int id) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		stockmateriaprima = new Stockmateriaprima();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from materiaprima where Id_materiaprima = ?");
			stmt.setLong(1, id);
			rs = stmt.executeQuery();

			if (rs.next()) {
			   stockmateriaprima.setNroMateriaprima(rs.getInt("Id_materiaprima"));
            stockmateriaprima.setStockactual(rs.getInt("Stockactual"));
			}

			return stockmateriaprima;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	@Override
	public boolean existInstance(Stockmateriaprima stockmateriaprima) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		boolean result = false;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from materiaprima where UCASE(nombre)");

//			stmt.setString(1, stockmateriaprima.getStockactual().toUpperCase());
//			stmt.setString(2, stockmateriaprima.getApellido().toUpperCase());
			rs = stmt.executeQuery();

			if (rs.next()) {
				result = true;
			}

			return result;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}
}
